package message.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import message.entity.Entity;

/**
 * @ClassName Data
 * @Description TODO
 * @Author lixianglong
 * @Date 2020年6月5日 下午9:04:48
 * @Version 1.0
 */
public class Data {
	String url="jdbc:mysql://localhost:3306/message";
	String username="perfect";
	String password="123";
	Connection con;
	PreparedStatement statement=null;
	public void data(Entity entity){
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
		con=DriverManager.getConnection(url,username,password);
		String sql="insert into t_entity(name,sex,profession) values(?,?,?)";
		statement=con.prepareStatement(sql);
		statement.setString(1, entity.getName());
		statement.setString(2, entity.getSex());
		statement.setString(3, entity.getProfession());
		statement.executeUpdate();
		}
		catch(ClassNotFoundException e){
			e.printStackTrace();
		}
		catch(SQLException e2) {
			e2.printStackTrace();
		}
		finally {
			if(statement!=null) {
				try {
					statement.close();
				}catch(SQLException e)
				{
					
				}
			}
			if(con!=null) {
				try {
					con.close();
				}catch(SQLException e)
				{
					
				}
			}
		}
		
	}
	
}
